      
<?php

	$text = '<html xmlns="http://www.w3.org/1999/xhtml"> 
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<a href="http://www.micronet.com/" target="_blank" style="color:#0000FF ; text-decoration:none">www.micronet.com</a> </p> 
	<a href="http://www.nair-co.com/" target="_blank" style="color:#0000FF; text-decoration:none">www.nair-co.com</a></p> 
	<a href="http://www.idatix.com/" target="_blank" style="color:#0000FF; text-decoration:none">iDatix </a></p>
	<a name="articles" id="articles"></a>
	www.ojoin.com <br><p>  http://www4.ojoin.com  </br>
	http://ojoin.com <br><p>  http://ww2.ojoin.com  </br>
	<script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js">
	<link rel="stylesheet" type="text/css" href="http://www.tizag.com/css/default-overflow.css" />
	<img src="http://www.ipoditunesongo.com/LeadForce1/lf1-newsletter/july06/images/articles-title.jpg" width="688" height="68" align="absbottom">
	<map name="Map" id="Map"><area shape="rect" coords="15,49,223,101" href="http://www.leadformix.com/" target="_blank" alt="logo" /> 
	<a href="mailto:info@sitime.com">info@sitime.com</a>.
	<a href="http://www.sitime.com/mysitime-registration/registers?t=1">http://www.sitime.com/mysitime-registration/registers</a>
	background: rgb(255, 0, 0) url(../res/img/temp/634043/original/cc3d8715eed0c.jpg) repeat fixed left top; cursor: auto;
	<div id="divbg" style="background-color:#ff0000"><img id="bg" src="../res/img/temp/634043/original/cc3d8715eed0c.jpg" width="100%" height="100%" /></div>
	background-image: url(../res/img/temp/634043/original/cc3d8715eed0c.png);
	background: rgb(255, 0, 0) url(http://google.com/res/img/temp/634043/original/cc3_d8715eed0c.jpg) repeat fixed left top; cursor: auto;
	background: rgb(255, 0, 0) url(https://google.com/res/img/temp/634043/original/cc3_d8715eed0c.jpg) repeat fixed left top; cursor: auto;';

	/*if (isset($_POST['inputText'])) {
        	$text = $_POST['inputText']; 
	}*/
	
	$text = htmlspecialchars_decode($text);
	
	
	/*================ START ANCHOR PARSING ===================*/
    
    $anchorpattern = '#<a\s
        (?:(?= [^>]* href="   (?P<href>  [^"]*) ")|)
        (?:(?= [^>]* title="  (?P<title> [^"]*) ")|)
        (?:(?= [^>]* target=" (?P<target>[^"]*) ")|)
        (?:(?= [^>]* style="  (?P<style> [^"]*) ")|)
        [^>]*>
        (?P<text>[^<]*)|<\/?(?:\b(?!)[^>]+?)></a>#xi';
  
  	 preg_match_all('#<a\s
        (?:(?= [^>]* href="   (?P<href>  [^"]*) ")|)
        (?:(?= [^>]* title="  (?P<title> [^"]*) ")|)
        (?:(?= [^>]* target=" (?P<target>[^"]*) ")|)
        (?:(?= [^>]* style="  (?P<style> [^"]*) ")|)
        [^>]*>
        (?P<text>[^<]*)|<\/?(?:\b(?!)[^>]+?)></a>#xi',  $text,  $ahref_matches,  PREG_SET_ORDER);
		
	$href_urlres="";
	$tracked_href_url='';
	foreach ($ahref_matches as $mtch) {
		$link = $mtch[1];
		if(isValidURL($link) and !isImage($link)) {		    
			print_r("\r\n valid---".$link);
			$href_url = trim($link);		
			$hrefurl = array();  	
			$hrefurl[]=$href_url;
			$result1 = preg_grep("/[\?|\&]t=1/",$hrefurl);
			if (count($result1)!='1'){
				$href_urlres .= $href_url . ",";
			}else{
				$tracked_href_url .= $href_url . ",";
			}
		}else{
			print_r("\r\ninvalid---".$link);
		}		
	}
	
	$str = substr($href_urlres,0,-1);

    $str = preg_replace("/^\s+$/", "", $str);
    $str = preg_replace("/[\n\r]/", "", $str);
     
    $text = preg_replace($anchorpattern, "", $text); 
    
    /*================ END ANCHOR PARSING ===================*/
	
	$result ="";
    $matches = array();
	// preg_match_all('|https*://[\d\w.\/;?:@&=+$,\-_!~*\'()#%]+|i', $text, $matches);
	//$pattern = "((href|https?):((//)|(\\\\))[\w\d:#%/;$()~_?\\-=\.&]*)";
	$pattern = "((((https?):((//)|(\\\\)))|(www.))[\w\d:#%/;$()~_?\\-=\.&]*)";
	preg_match_all($pattern, $text, $matches);
	$urlArray = $matches[0];
	$validUrlArray = array();
	//echo "\r\n-----------------------";
	foreach($urlArray as $url){
		if(isValidURL($url) and !isImage($url)) {
			$validUrlArray[] = $url;
			$result .= $url . ",";
			print_r("\r\nvalid---".$url);
		}else{
			print_r("\r\ninvalid---".$url);
		}		
	}
	//echo "\r\n-----------------------\r\n";
	$result1 = preg_grep("/[\?|\&]t=1/",$validUrlArray);
	$result2 = preg_grep("/[\?|\&]t=1/",$validUrlArray,PREG_GREP_INVERT);
 
	$string1 = join(",",$result1);
	$string1 = preg_replace("/^\s+$/", "", $string1);
	$string1 = preg_replace("/[\n\r]/", "", $string1);
	    
	$string2 = join(",",$result2);
	$string2 = preg_replace("/^\s+$/", "", $string2);
		
	if (strlen($string2)==0){ 
    	$string2 = $str;    
  	}else{
    	$str = ','.$str;
    	$string2 = $string2.$str;
  	}

  	if (strlen($tracked_href_url)>0){
    	$tracked_href_url = substr($tracked_href_url ,0,-1);
    	$string1 = $tracked_href_url;
  	}  
	
   	$res = $string1."--*--".$string2;
	$res = str_replace("\n", "", $res);
	print $res;
    
	function isImage( $url )
	{
		$pos = strrpos( $url, ".");
			if ($pos === false)
			  return false;
			$ext = strtolower(trim(substr( $url, $pos)));
			$imgExts = array(".js", ".css", ".gif", ".jpg", ".jpeg", ".png", ".tiff", ".tif", ".swf", ".pdf", 
			".gif)", ".jpg)", ".jpeg)", ".png)", ".tiff)", ".tif)", ".swf)", ".pdf)"); // this is far from complete but that's always going to be the case...
			if ( in_array($ext, $imgExts) )
			  return true;
		return false;
	}
		
	function isValidURL($url){
		// try to match url pattern
		$pattern = "((((https?):((//)|(\\\\)))|(www.))[\w\d:#%/;$()~_?\\-=\.&]*)i";
		if(preg_match($pattern, $url))  {
		    if(preg_match("/^http:\/\/www.w3.org/i", $url)) {    // http://www.w3.org - is w3 startand url
				return false;
			}			
			return true;
        }
		else {
			return false;     
        }
	}
?>
